/**
 * Project: ld.biz.web
 * 
 * File Created at 2012-9-23
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.web.action.autocode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liangdev.biz.dataobject.autocode.TemplateInfoDO;
import com.liangdev.biz.query.autocode.TemplateInfoQuery;
import com.liangdev.biz.service.autocode.TemplateInfoService;
import com.liangdev.biz.web.util.ActionUtil;
import com.liangdev.common.util.StringUtil;
import com.liangdev.web.vo.CommonJsonVO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 获取模板配置信息
 * 
 * @author chongan.wangca
 */
public class TemplateInfoAction extends ActionSupport {

    private static final long   serialVersionUID = -5147166361020554487L;

    private final Logger        logger           = LoggerFactory
                                                         .getLogger(TemplateInfoAction.class);

    @Resource
    private ActionUtil          actionUtil;

    @Resource
    private TemplateInfoService templateInfoService;

    public void getTemplateGroupJson() {

        List<String> groupNames = templateInfoService.queryGroupNameDistinct();
        try {
            String json = makeGroupNameJson(groupNames);
            actionUtil.getResponse().getWriter().print(json);
        } catch (IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 通过分组获取模板信息
     */
    public void getTemplatesByGroupNameJson() {

        String groupName = actionUtil.getRequestParamString("groupName");
        if (StringUtil.isBlank(groupName)) {
            return;
        }
        TemplateInfoQuery templateInfoQuery = new TemplateInfoQuery();
        templateInfoQuery.setGroupName(groupName);
        List<TemplateInfoDO> templateInfoDOList = templateInfoService.findList(templateInfoQuery);
        try {
            String json = makeTemplateItemsJson(templateInfoDOList);
            actionUtil.getResponse().getWriter().print(json);
        } catch (IOException e) {
            logger.error("", e);
        }
    }

    private String makeTemplateItemsJson(List<TemplateInfoDO> templateInfoDOList) {

        //        [
        //         {
        //            xtype: 'checkboxgroup',
        //            defaultType: 'checkboxfield',
        //            layout:'column',
        //            items: [
        //                {
        //                    boxLabel  : 'Anchovies',
        //                    name      : 'topping',
        //                    inputValue: '1',
        //                    id        : 'checkbox1',
        //                    padding   : '0 8 0 0' ,
        //                    tooltip   : 'Edit'
        //          }]}]

        StringBuffer json = new StringBuffer();
        json.append("[");

        for (int i = 0; i < templateInfoDOList.size(); i++) {
            TemplateInfoDO templateInfoDO = templateInfoDOList.get(i);
            json.append("{");
            json.append("boxLabel:");
            json.append("'" + templateInfoDO.getTemplateName() + "',");
            json.append("checked:true,");
            json.append("name:");
            json.append("'templateName_" + templateInfoDO.getId() + "',");
            json.append("inputValue:");
            json.append("'" + templateInfoDO.getId() + "',");
            json.append("id:");
            json.append("'" + templateInfoDO.getId() + "|" + templateInfoDO.getTemplateName()
                    + "',");
            json.append("padding:'0 8 0 0',");
            json.append("tooltip:'Edit'");
            json.append("}");
            if (i < templateInfoDOList.size() - 1) {
                json.append(",");
            }
        }
        json.append("]");
        System.out.println(json.toString());
        return json.toString();
    }

    /**
     * 转化为JSON
     * 
     * @param dbInfoDOList
     * @return
     */
    private String makeGroupNameJson(List<String> groupNames) {

        if (groupNames == null || groupNames.size() == 0) {
            return null;
        }

        List<CommonJsonVO> CommonJsonVOList = new ArrayList<CommonJsonVO>();
        for (String groupName : groupNames) {
            CommonJsonVO commonJsonVO = new CommonJsonVO();
            commonJsonVO.setId(groupName);
            commonJsonVO.setName(groupName);
            CommonJsonVOList.add(commonJsonVO);
        }
        JSONArray ja = JSONArray.fromObject(CommonJsonVOList);
        return ja.toString();
    }

}
